package com.zzyl.nursing.service.impl;

import java.util.List;

import Dto.NursingPlanDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author zc
 * @date 2025-05-24
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan selectNursingPlanById(Long id)
    {
                return getById(id);
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        if(nursingPlan.getPlanName() != null){
            queryWrapper.like(NursingPlan::getPlanName,nursingPlan.getPlanName());
        }
        if(nursingPlan.getStatus() != null){
            queryWrapper.eq(NursingPlan::getStatus,nursingPlan.getStatus());
        }
        return list(queryWrapper);

    }

    /**
     * 新增护理计划
     *
     * @param护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        // 保存护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insert(nursingPlan);

        // 保存计划
        if (dto.getProjectPlans() == null || dto.getProjectPlans().isEmpty()) {
            return 0;
        }

        List<NursingProjectPlan> projectPlans = dto.getProjectPlans().stream()
                .map(projectPlanDto -> {
                    NursingProjectPlan projectPlan = new NursingProjectPlan();
                    BeanUtils.copyProperties(projectPlanDto, projectPlan);
                    return projectPlan;
                })
                .collect(Collectors.toList());

        int count = nursingProjectPlanMapper.batchInsert(projectPlans, nursingPlan.getId());
        return count == 0 ? 0 : 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
                nursingPlan.setUpdateTime(DateUtils.getNowDate());
                return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
                return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
                return removeById(id) ? 1 : 0;
    }
}